using NuGet.Versioning;

using NuGetUpdater.Core.Analyze;

using Xunit;

namespace NuGetUpdater.Core.Test.Analyze;

public class SecurityVulnerabilityExtensionsTests
{
    [Fact]
    public void VersionInSafeVersions_IsNotVulnerable()
    {
        var version = NuGetVersion.Parse("1.0.1");
        var vulnerability = new SecurityVulnerability
        {
            DependencyName = "Dependency",
            PackageManager = "PackageManager",
            SafeVersions = [Requirement.Parse("> 1.0.0")],
            VulnerableVersions = [Requirement.Parse("<= 1.0.0")],
        };

        var result = vulnerability.IsVulnerable(version);

        Assert.False(result);
    }

    [Fact]
    public void VersionInVulnerableVersions_IsVulnerable()
    {
        var version = NuGetVersion.Parse("1.0.0");
        var vulnerability = new SecurityVulnerability
        {
            DependencyName = "Dependency",
            PackageManager = "PackageManager",
            SafeVersions = [Requirement.Parse("> 1.0.0")],
            VulnerableVersions = [Requirement.Parse("<= 1.0.0")],
        };

        var result = vulnerability.IsVulnerable(version);

        Assert.True(result);
    }

    [Fact]
    public void VersionNotInVulnerableVersions_IsNotVulnerable()
    {
        var version = NuGetVersion.Parse("1.0.1");
        var vulnerability = new SecurityVulnerability
        {
            DependencyName = "Dependency",
            PackageManager = "PackageManager",
            SafeVersions = [],
            VulnerableVersions = [Requirement.Parse("<= 1.0.0")],
        };

        var result = vulnerability.IsVulnerable(version);

        Assert.False(result);
    }

    [Fact]
    public void VersionNotInSafeVersions_IsVulnerable()
    {
        var version = NuGetVersion.Parse("1.0.0");
        var vulnerability = new SecurityVulnerability
        {
            DependencyName = "Dependency",
            PackageManager = "PackageManager",
            SafeVersions = [Requirement.Parse("> 1.0.0")],
            VulnerableVersions = [],
        };

        var result = vulnerability.IsVulnerable(version);

        Assert.True(result);
    }
}
